using System.Data.SqlClient;
using CoreLibrary.Sql;

namespace IBBI.DataBase.Impl.Sql {

    internal class DeleteCategory : ISqlUpdator<bool> {

        private readonly int _id;
        private readonly int _width;
        private readonly int _right;
        private readonly string _tableName;

        public DeleteCategory(int id, int width, int right, string tableName) {
            _id        = id;
            _width     = width;
            _right     = right;
            _tableName = tableName;
        }

        public string GetSQL() {
            return string.Format(
               @"
                 UPDATE {0} SET RID = RID - @width WHERE RID > @right;
                 UPDATE {0} SET LID = LID - @width WHERE LID > @right;
                 DELETE FROM {0} WHERE ID = @id;
               ", 
                _tableName
            );
        }

        public void SetParameters(SqlCommand command) {
            command.Parameters.AddWithValue("@id",     _id);
            command.Parameters.AddWithValue("@width",  _width);
            command.Parameters.AddWithValue("@right",  _right);
        }

        public bool GetResult(SqlConnection conn, int result) {
            return result >= 0;
        }

    }

}